package woodnaonly.radiobutto_test;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;

import woodnaonly.radiobutto_test.Fragment.ImageDetailFragment;

public class ImagePagerActivity extends AppCompatActivity
{
    final public static String LIST = "list";
    final public static String POSITION = "position";
    private ArrayList<String> mSelectPath;
    private int position;
    private ViewPager mViewPager;
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_pager);
        Intent intent = getIntent();
        mSelectPath = intent.getStringArrayListExtra(LIST);
        position = intent.getIntExtra(POSITION, -1);
        initviewpager();
    }

    private void initviewpager()
    {

        mTextView = (TextView) findViewById(R.id.indicator);
        mTextView.setText((position + 1) + "/" + mSelectPath.size());
        mViewPager = (ViewPager) findViewById(R.id.imageviewpager);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public Fragment getItem(int position)
            {
                return ImageDetailFragment.newInstance(mSelectPath.get(position));
            }

            @Override
            public int getCount()
            {
                return mSelectPath.size();
            }
        });
        mViewPager.setCurrentItem(position);
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageSelected(int position)
            {
                mTextView.setText((position + 1) + "/" + mSelectPath.size());
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });
    }
}
